package com.ruoyi.web.controller.pay;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.ijpay.alipay.AliPayApi;
import com.ruoyi.pay.entity.PayNotifyRecord;
import com.ruoyi.pay.handler.PayNotifyCallbakHandler;
import com.ruoyi.pay.service.PayNotifyRecordService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 通知记录日志Controller
 *
 * @author ruoyi
 * @date 2020-10-10
 */
@Controller
@Slf4j
@RequestMapping("/system/record")
public class PayNotifyRecordController extends BaseController
{
    private String prefix = "system/record";

    @Autowired
    private PayNotifyRecordService payNotifyRecordService;
    @Autowired
    private  PayNotifyCallbakHandler alipayCallback;
    @Autowired
    private  PayNotifyCallbakHandler weChatCallback;
    @RequiresPermissions("system:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询通知记录日志列表
     */
    @RequiresPermissions("system:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PayNotifyRecord payNotifyRecord)
    {
        startPage();
        List<PayNotifyRecord> list = payNotifyRecordService.list( Wrappers.query(payNotifyRecord));
        return getDataTable(list);
    }

    /**
     * 导出通知记录日志列表
     */
    @RequiresPermissions("system:record:export")
    @Log(title = "通知记录日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PayNotifyRecord payNotifyRecord)
    {
        List<PayNotifyRecord> list = payNotifyRecordService.list( Wrappers.query(payNotifyRecord));
        ExcelUtil<PayNotifyRecord> util = new ExcelUtil<PayNotifyRecord>(PayNotifyRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 新增通知记录日志
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存通知记录日志
     */
    @RequiresPermissions("system:record:add")
    @Log(title = "通知记录日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PayNotifyRecord payNotifyRecord)
    {
        return toAjax(payNotifyRecordService.save(payNotifyRecord));
    }

    /**
     * 修改通知记录日志
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PayNotifyRecord payNotifyRecord = payNotifyRecordService.getById(id);
        mmap.put("payNotifyRecord", payNotifyRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存通知记录日志
     */
    @RequiresPermissions("system:record:edit")
    @Log(title = "通知记录日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PayNotifyRecord payNotifyRecord)
    {
        return toAjax(payNotifyRecordService.updateById(payNotifyRecord));
    }

    /**
     * 删除通知记录日志
     */
    @RequiresPermissions("system:record:remove")
    @Log(title = "通知记录日志", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(payNotifyRecordService.removeByIds(Arrays.asList(ids.split(","))));
    }


    /**
     * 支付宝渠道异步回调
     *
     * @param request 渠道请求
     * @return
     */
    @SneakyThrows
    @RequestMapping("/ali/callbak")
    public void aliCallbak(HttpServletRequest request, HttpServletResponse response) {
        // 解析回调信息
        Map<String, String> params = AliPayApi.toMap(request);

        try {
            response.getWriter().print(alipayCallback.handle(params));
            log.info("支付宝回调处理成功");
        } catch (IOException e) {
            log.info("支付宝回调处理失败");
            e.printStackTrace();
        }
    }

    /**
     * 微信渠道支付回调
     *
     * @param request
     * @return
     */
    @SneakyThrows
    @ResponseBody
    @PostMapping("/wx/callbak")
    public String wxCallbak(HttpServletRequest request) {
        //String xmlMsg = HttpKit.readData(request);
        //log.info("微信订单回调信息:{}", xmlMsg);
        //Map<String, String> params = PaymentKit.xmlToMap(xmlMsg);
        //return weChatCallback.handle(params);
        return "";
    }
    /**
     * 微信渠道支付回调
     *
     * @param request
     * @return
     */
    @SneakyThrows
    @GetMapping("/paySuccess")
    public String paySuccess(HttpServletRequest request) {
        return prefix + "/success";
    }
}
